﻿using System;
using System.Diagnostics;
using System.Web.Mvc;

namespace ControllerExtensibility.Controllers
{
    public class TimeElapsedProfileAttribute : ActionFilterAttribute
    {
        private Stopwatch _timer;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            _timer = Stopwatch.StartNew();
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            _timer.Stop();
            filterContext.HttpContext.Response.Write(String.Format("<div>Total elapsed time(ms): {0:F6} </div>", _timer.ElapsedMilliseconds));
        }


    }
}